/**
 * @author gc291
 */

var TIS = TIS ||
{};

(function($){
	
	$(document).ready(function(){
	
		$(".predicateor").bind("click", function(event){
			if ($('#Rule-ruletext').val() == "") {
				$('#Rule-ruletext').val($(this).attr('predicate'));
			} else {
				$('#Rule-ruletext').val("( " + $('#Rule-ruletext').val() + " OR " + $(this).attr('predicate') + " )");
			}
			var test = $.makeArray($('#Rule-ruletext').val());
			console.log(test);
		});
		
		$(".predicateand").bind("click", function(event){
			if ($('#Rule-ruletext').val() == "") {
				$('#Rule-ruletext').val($(this).attr('predicate'));
			} else {
				$('#Rule-ruletext').val("( " + $('#Rule-ruletext').val() + " AND " + $(this).attr('predicate') + " )");
			}
		});
		
		
	    $("form input.course").bind("click", function(event){
			event.preventDefault();
			var input = $(this);
			
			var id = $(this).attr('courseid');
	
	    	// If the checkbox is unchecked
	        if (input.is(':checked')) {
				var myObj = $(this);
	            $.ajax({
	                type: "POST",
	                url: "admin.php?manage=Predicate&action=edit&id="+id,
	                data: ({
	                    addCourse: input.attr('value')
	                }),
	                success: function(response){
						input[0].checked = true;
						$('#messages').html(response);
						input.parent().css("background-color:green;");
						$('.ui-state-highlight').effect("highlight", {}, 3000);
						$('.ui-state-highlight').hide("drop", { direction: "up" }, 1000);
	                },
					error: function(response){
						$('#messages').html(response.responseText);
						$('.ui-state-error').effect("highlight", {}, 3000);
					}
						
	            });
	
	        // If the checkbox is already checked
	        } else {
	            
	            $.ajax({
	                type: "POST",
	                url: "admin.php?manage=Predicate&action=edit&id="+id,
	                data: ({
	                    removeCourse: input.attr('value')
	                }),
	                success: function(response){
						input[0].checked = false;
						$('#messages').html(response);
						$('.ui-state-highlight').effect("highlight", {}, 3000);
						$('.ui-state-highlight').hide("drop", { direction: "up" }, 1000);
					},
					error: function(response){
						$('#messages').html(response.responseText);
						$('.ui-state-error').effect("highlight", {}, 3000);
						
					}
	            });
	        };
			
		});
	});
}(jQuery));